package com.ky.manage.activity.outdoor;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.ky.manage.BaseApplication;
import com.ky.manage.R;
import com.ky.manage.base.BaseActivity;
import com.ky.manage.model.req.OutdoorSecureAddReq;
import com.ky.manage.ui.DrawableTextView;
import com.ky.manage.utils.ScreenUtils;
import com.ky.manage.utils.ZyUtils;
import com.ky.manage.utils.http.AsyncHttpUtils;
import com.ky.manage.utils.http.BusinessHttpReqUtils;
import com.loopj.android.http.TextHttpResponseHandler;

import cz.msebera.android.httpclient.Header;

/**
 * 新增户外检修
 */
public class OutdoorAddSecurityCheckActivity extends BaseActivity {
    private DrawableTextView selectRegionTv,  isImportRegionTv, isNeedMendTv, checkResultTv;
    private int popType = 0;
    public static final int POP_TYPE_IS_IMPORT_REGION = 1;
    public static final int POP_TYPE_IS_NEED_MEND = 2;
    public static final int POP_TYPE_CHECK_RESULT = 3;
    private OutdoorSecureAddReq outdoorSecureAddReq = new OutdoorSecureAddReq();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_outdoor_add_security_check;
    }

    @Override
    protected void initView() {
        findTextView(R.id.page_title_tv, "新增户外检修记录");
        findView(R.id.back_iv, v -> {
            finish();
        });
        findView(R.id.submit_tv, v -> {
            //下一步点击

        });
        selectRegionTv = findView(R.id.select_region_tv, v -> {
           //选择检修区域
            // TODO: 2021/9/17 接口定义不清楚
        });
        isImportRegionTv = findTextView(R.id.is_import_region_tv, "0".equals(outdoorSecureAddReq.isEmphasis) ? "是" :"否", v -> {
           //是否为重点检修区域
            popType = POP_TYPE_IS_IMPORT_REGION;
            //是否重点巡检线路（0是， 1否）
            showSelectPop(isImportRegionTv, v1 -> {
                isImportRegionTv.setText("是");
                outdoorSecureAddReq.isEmphasis = "0";
                ZyUtils.getInstance().hideCustomPop();
            }, v2 -> {
                isImportRegionTv.setText("否");
                outdoorSecureAddReq.isEmphasis = "1";
                ZyUtils.getInstance().hideCustomPop();
            });
        });
        isNeedMendTv = findTextView(R.id.is_need_mend_tv, "0".equals(outdoorSecureAddReq.isMend) ? "是" :"否");
        /*, v -> {
           //是否需要整改
            popType = POP_TYPE_IS_NEED_MEND;
            //是否整改（0是， 1否）
            showSelectPop(isNeedMendTv, v1 -> {
                isNeedMendTv.setText("是");
                outdoorSecureAddReq.isMend = "0";
                ZyUtils.getInstance().hideCustomPop();
            }, v2 -> {
                isNeedMendTv.setText("否");
                outdoorSecureAddReq.isMend = "1";
                ZyUtils.getInstance().hideCustomPop();
            });
        }); */
        checkResultTv = findTextView(R.id.check_result_tv, "0".equals(outdoorSecureAddReq.isMend) ? "正常" :"不正常");
        /*, v -> {
           //检测结果是否正常
            popType = POP_TYPE_CHECK_RESULT;
            showSelectPop(checkResultTv, v1 -> {
                checkResultTv.setText("正常");
                outdoorSecureAddReq.checkResult = "0";
                ZyUtils.getInstance().hideCustomPop();
            }, v2 -> {
                checkResultTv.setText("不正常");
                outdoorSecureAddReq.checkResult = "1";
                ZyUtils.getInstance().hideCustomPop();
            });
        });*/

    }

    @Override
    protected void initData() {
        requestPointList();
    }

    /**
     * 选择pop
     * @param targetView
     * @param yesListener
     * @param noListener
     */
    public void showSelectPop(View targetView, View.OnClickListener yesListener, View.OnClickListener noListener) {
        View popContentView = LayoutInflater.from(this).inflate(R.layout.pop_is_import_checck_region, null);
        TextView yesTv = popContentView.findViewById(R.id.yes_tv);
        yesTv.setOnClickListener(yesListener);
        TextView noTv = popContentView.findViewById(R.id.no_tv);
        noTv.setOnClickListener(noListener);
        switch (popType) {
            case POP_TYPE_IS_IMPORT_REGION:
                //是否为重点区域
            case POP_TYPE_IS_NEED_MEND:
                //是否需要整改
                yesTv.setText("是");
                noTv.setText("否");
                break;
            case POP_TYPE_CHECK_RESULT:
                //是否正常
                yesTv.setText("正常");
                noTv.setText("不正常");
                break;
            default:
                break;
        }
        ZyUtils.getInstance().showCustomPop(this, targetView, popContentView, null,
                ScreenUtils.getScreenW(), 0, 0);
    }


    private void requestPointList() {
        int pointId = BaseApplication.getLoginUserInfo().businessOutletsId;
        Log.d("lhm", "pointId = " + pointId);
        AsyncHttpUtils.getInstance().get(BusinessHttpReqUtils.SERVICE_DOMAIN + "/appApi/getPointList?outdoorId=" + pointId, new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                Log.d("lhm", "requestPointList onFailure= " + responseString);
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                Log.d("lhm", "requestPointList onSuccess = " + responseString);
            }
        });
    }
}